/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package EventProcessor;

import gamecontrollerserver.GameControllerServerApp;
import java.awt.MouseInfo;
import java.awt.Robot;
import javax.swing.JFrame;

/**
 *
 * @author luc0
 */
public class MouseEventProcessor {

    private SmoothMover smoothMover;
    private boolean started = false;
    public MouseEventProcessor() {
        smoothMover = new SmoothMover(GameControllerServerApp.getApplication().getMainFrame());
    }


    public void moveMouse(int x, int y, int duration) {
        smoothMover.mouseMove(x, y, 100);
    }

    public void endMouse() {
        smoothMover.endMouseMovement();
    }

    class SmoothMover {

        private Robot robot;
        private int currentX;
        private int currentY;
        private JFrame owner;
        private int moveToX;
        private int moveToY;
        public SmoothMover(JFrame frame) {
            owner = frame;

            try {
                robot = new Robot();
            } catch (Exception e) {
                System.out.println("Uh-oh...");
                e.printStackTrace();
            }
        }

        public void endMouseMovement(){
            started = false;
            moveToX = 0;
            moveToY = 0;
        }

        public Robot getRobot() {
            return robot;
        }

        private void mouseMoveLoop(){
            while(started){
            
           currentX = (int) MouseInfo.getPointerInfo().getLocation().getX();
           currentY = (int) MouseInfo.getPointerInfo().getLocation().getY();
           robot.mouseMove(currentX+moveToX, currentY + moveToY);
           robot.delay(4);
            }
        }

        public void mouseMove(int destX, int destY, int duration) {

            if(!started){
                started = true;
                mouseMoveLoop();
            }
            destX = destX - 65;

            destY = destY - 65;

            System.err.println("destX:"+destX+" destY:"+destY);

            // X negative
            if(destX<=-15){
                moveToX = -3;
            } else if(destX<=-10){
                moveToX = -2;
            }else if(destX<=-2){
                moveToX = -1;
            } else if(destX>=15){
                // X positiv
                moveToX = 3;
            } else if(destX>=10){
                moveToX = 2;
            }else if(destX>=3){
                moveToX = 1;
            } else {
                moveToX = 0;
            }


           // Y negative
           if(destY<=-15){
                moveToY = -3;
            } else if(destY<=-10){
                moveToY = -2;
            }else if(destY<=-5){
                moveToY = -1;
            } else if(destY>=15){
                // Y positiv
                moveToY = 3;
            } else if(destY>=10){
                moveToY = 2;
            }else if(destY>=5){
                moveToY = 1;
            } else {
                moveToY = 0;
            }


            System.err.println("MoveToX:"+moveToX+" moveToY:"+moveToY);

//            currentX = (int) MouseInfo.getPointerInfo().getLocation().getX() + destX;
//            currentY = (int) MouseInfo.getPointerInfo().getLocation().getY() + destY;
//
//
//            int x = currentX;
//            int y = currentY;
//            System.out.println("x:" + x + " y:" + y);

//            int steps = 100;
//            int sleepTime = duration / steps;
//            for (int i = 0; i <= steps; i++) {
//                float a = (float) i / steps;
//                x = currentX + (int) (a * destX);
//                y = currentY + (int) (a * destY);
//                robot.mouseMove(x, y);
//                System.out.println("sleepTime:"+sleepTime);
//                robot.delay(sleepTime);
//            }
//            currentX = x;
//            currentY = y;
//---------------------------
//            destX = destX - 65;
//
//            destY = destY - 65;
//
//            currentX = (int) MouseInfo.getPointerInfo().getLocation().getX() + destX;
//            currentY = (int) MouseInfo.getPointerInfo().getLocation().getY() + destY;
//
//
//            int x = currentX;
//            int y = currentY;
//            System.out.println("x:" + x + " y:" + y);
//
//            int steps = 100;
//            int sleepTime = duration / steps;
//            for (int i = 0; i <= steps; i++) {
//                float a = (float) i / steps;
//                x = currentX + (int) (a * destX);
//                y = currentY + (int) (a * destY);
//                robot.mouseMove(x, y);
//                robot.delay(sleepTime);
//            }
//            currentX = x;
//            currentY = y;


        }
    }
}
